package com.cowcode.controller;

import com.cowcode.biz.CollectBiz;
import com.cowcode.biz.QuestionBiz;
import com.cowcode.biz.StudyBiz;
import com.cowcode.biz.TextBiz;
import com.cowcode.entity.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * User: ljy
 * Date: 2021/6/21
 * Time: 20:43
 */
@Controller
@RequestMapping("jump/")
public class JumpController {
    @Resource
    private TextBiz textBiz;
    @Resource
    private QuestionBiz questionBiz;
    @Resource
    private StudyBiz studyBiz;
    @Resource
    private CollectBiz collectBiz;

    @RequestMapping("toindex")
    public String toindex(Model model){
        List<Study> slist = studyBiz.queryAll();
        model.addAttribute("slist",slist);
        model.addAttribute("jump","toindex");
        getMsg(model);
        return "../index";
    }
    @RequestMapping("tostudy")
    public String tostudy(Model model){
        getMsg(model);
        model.addAttribute("jump","tostudy");
        return "study";
    }
    @RequestMapping("toclass")
    public String toclass(Model model){
        getMsg(model);
        model.addAttribute("jump","toclass");
    return "class";
    }
    @RequestMapping("toarticles")
    public String toarticles(Model model){
        getMsg(model);
        model.addAttribute("jump","toarticles");
    return "articles";
    }
    @RequestMapping("toarticle")
    public String toarticle(Model model){
        getMsg(model);
        model.addAttribute("jump","toarticle");
    return "article";
    }
    @RequestMapping("tofaq")
    public String tofaq(Model model, HttpSession session){
        User user = (User) session.getAttribute("user");
        model.addAttribute("user",user);
        model.addAttribute("jump","tofaq");
        List<Text> alist = new ArrayList<>();//回答问题的集合
        List<Question> qlist = questionBiz.queryByPage(0,6);//问题的集合
        model.addAttribute("qlist",qlist);
        if (qlist!=null){
            for (Question question : qlist) {
                List<Text> textList = textBiz.queryByQid(question.getQid());
                for (Text text : textList) {
                    alist.add(text);
                }
            }
        }
        model.addAttribute("alist",alist);
        List<Study> slist = studyBiz.queryAll();//学习语言的集合
        model.addAttribute("slist",slist);
        String msg = (String) model.getAttribute("msg");
        model.addAttribute("msg",msg);
        int count = questionBiz.getCount();
        int pagecount = count % 6 == 0 ? (count / 6) : (count / 6 + 1);
        model.addAttribute("pagecount",pagecount);//总页码
        model.addAttribute("count",questionBiz.getCount());//总条数
        model.addAttribute("index",1);//首页
        String index2 = Integer.toBinaryString(1);//把数字转换成二进制格式显示在界面上
        String pagecount2 = Integer.toBinaryString(pagecount);
        model.addAttribute("index2",index2);
        model.addAttribute("pagecount2",pagecount2);
        getMsg(model);
        List<Collect> clist = null;//收藏夹的集合
        if (user!=null){
            clist = collectBiz.queryByUid(user.getUid());
        }
        model.addAttribute("clist",clist);
        return "faq";
    }
    @RequestMapping("toteacher")
    public String toteacher(Model model){
        getMsg(model);
        model.addAttribute("jump","toteacher");
    return "teacher";
    }
    @RequestMapping("towallets")
    public String towallets(Model model){
        getMsg(model);
        model.addAttribute("jump","towallets");
    return "wallets";
    }
    @RequestMapping("tologin")
    public String tologin(Model model){
        getMsg(model);
        model.addAttribute("jump","tologin");
    return "login";
    }
    @RequestMapping("tosignup")
    public String tosignup(Model model){
        getMsg(model);
        model.addAttribute("jump","tosignup");
    return "signup";
    }
    @RequestMapping("touser")
    public String touser(Model model,HttpSession session){
        User user = null;
        user = (User) session.getAttribute("user");
        if (user==null){
            model.addAttribute("msg","请登录后再查看个人信息");
            return "login";
        } else {
            model.addAttribute("user",session.getAttribute("user"));
        }
        List<Collect> clist = null;
        clist = collectBiz.queryByUid(user.getUid());
        model.addAttribute("clist",clist);
        List<Text> tlist = new ArrayList<>();
        if (clist!=null){
            for (Collect collect : clist) {
                List<Text> textList = textBiz.queryByQid(collect.getQid().getQid());
                for (Text text : textList) {
                    tlist.add(text);
                }
            }
        }
        model.addAttribute("tlist",tlist);
        getMsg(model);
        model.addAttribute("jump","touser");
    return "user";
    }

    private void getMsg(Model model){
        String msg = (String) model.getAttribute("msg");
        model.addAttribute("msg",msg);
    }
}
